#ifndef UTILS_H_INCLUDED
#define UTILS_H_INCLUDED

#include <string>
#include <stdio.h>

using namespace std;

class Utils{
public:
    static string binaryBlock2String(int numofBytes, char * buffer){
        int intLen = sizeof(int);
        string res = "";
        char temp = '\0';
        for(int i = numofBytes - 1; i >= 0; i--){
            temp = buffer[i];
            for(int j = 0; j < 8; j++){
                if((temp & 1) == 0){
                    res = "0" + res;
                }else{
                    res = "1" + res;
                }
                temp>>=1;
            }
        }
        return res;
    }
    static int string2binaryBlock(string source, char *& buffer){
        int strLen = source.size();
        int len = strLen / 8;
        buffer = new char[len];
        const char * s = source.c_str();
        int k = strLen - 1;
        for(int i = len - 1; i >= 0; i--){
            buffer[i] = 0;
            for(int j = 7; j >= 0; j--){
                buffer[i] += ((s[k--] - '0')<<(7 - j));
            }
        }
        return len;
    }
    void getKey(string filePath, string &key){
        ifstream ifile(filePath.c_str());
        char * cstr = new char[70];
        if(ifile.is_open()){
            ifile.getline(cstr,65);
            key = cstr;
        }else{
            cout<<"error opening keys file!";
            exit(0);
        }
        ifile.close();
    }
    string XOR(string str1, string str2){
        int len = str1.size();
        string res = "";
        for(int i = 0; i < len; i++){
            res.append(str1[i] == str2[i] ? "0" : "1");
        }
        return res;
    }
};

#endif // UTILS_H_INCLUDED

